package com.android.jump;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

import com.android.jump.jumper.DrawableObject;
import com.android.jump.jumper.Jumper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Background extends DrawableObject
{
	 private int distance = 0;
	
	 private Bitmap mBmpCloud = null;
	 private Paint mPaint = null;
	 private int mTopPos = 440;
	 private Animation mCloudfall = null;
	 private Animation mClouddepose = null;
	 
	 private Vector<Cloud> mvCloud = null;
	 private boolean bFirstTime = true;
	 
	 private int mScore = 0;
	 
	 Background(Bitmap bg, Animation cloudfall,Animation clouddepose)
	 {
		 
	     super(bg);
	     mCloudfall = cloudfall;
	     mClouddepose = clouddepose;
	     mPaint = new Paint();
         mvCloud = new Vector<Cloud>();
         
         //mBmpCloud = cloud;
         Cloud c = new Cloud(mBmpCloud,cloudfall,clouddepose);
         mvCloud.add(c);
         
         setPosition(0, 0);
	 }
	 
	 public void Update(int nChange)
	 {
	     mTopPos += nChange;
	     if (mTopPos >= getBitmap().getHeight()) {
	         mTopPos = 0;
	         bFirstTime = false;
	     }
		 
    	 for(int i=0;i<mvCloud.size();i++)
    	 {
    		 mvCloud.get(i).update(nChange);
    		 
    		 if(mvCloud.get(i).isOutofDate())
    		 { mvCloud.remove(i);}
    	 }
		 
    	 randomCloud(nChange);
    	 mScore += nChange;
	 }
	 
	 public void doDraw(Canvas nCanvas)
	 {
		 if (bFirstTime) {
		     nCanvas.drawBitmap(getBitmap(), 0, mTopPos, mPaint);
		     nCanvas.drawBitmap(getBitmap(), 0, mTopPos - getBitmap().getHeight(), mPaint);
		 }
		 else {
			 nCanvas.drawBitmap(getBitmap(), 0, 0, mPaint);
		 }
	     
	     Paint paint = new Paint();
	     paint.setColor(Color.BLACK);
	     paint.setFakeBoldText(true);
	     nCanvas.drawText("Score: " + String.valueOf(mScore / 10), 10, 10, paint);
	     
    	 for(int i = 0; i < mvCloud.size(); i++) {
    		 mvCloud.get(i).doDraw(nCanvas);
    	 }
	 }

	 public void Cloudfall(int nChange)
	 {
		 for(int i=0;i<mvCloud.size();i++)
    	 {
    		 mvCloud.get(i).update(nChange);
    		 
    		 if(mvCloud.get(i).isOutofDate())
    		 { mvCloud.remove(i);}
    	 }
		 randomCloud(nChange);
	 }
	 
	 private void randomCloud(int nChange)
	 {
		 distance+=nChange;
		 
		 int a = (int)(Math.random()*700);
		 int y = (int)(Math.random()*300);

		 if(a<distance&&a>30)
		 {
			 Cloud c = new Cloud(mBmpCloud,mCloudfall,mClouddepose);
			 c.setPosition(y, 0);
	         mvCloud.add(c);
	         distance = 0;
		 }
		 else
			 return;
	 }
	 
	 
	 public boolean updatePhysics(Jumper jumper, double elapsed) {
	     boolean ret = false;
	     for (int i = 0; i < mvCloud.size(); i++) {
	         if (ret = ret || mvCloud.get(i).isCollide(jumper)) {
	             jumper.setVelocity(jumper.getVelocityX(), -130.0);
	             mvCloud.get(i).DepoAni();
	             mvCloud.remove(i);
	             jumper.setAcceleration(getAccelerationX(), 40.0);
	             break;
	         }
         }
         jumper.updatePosition(elapsed);
         
         //Log.i("Position", String.valueOf(jumper.getLastOffsetY()));
         return ret;
	 }
	 
	 public int getScore() {
	     return mScore;
	 }
	 
	 public void restore() {
	     distance = 0;
	     mTopPos = 440;
	     mScore = 0;
	     mvCloud.clear();
	     bFirstTime = true;
	 }
	 
    public void saveHighScore(GameView view) {
	    ArrayList<Integer> list = new ArrayList<Integer>();
        try
        {
            FileInputStream fis = view.getContext().openFileInput("hs");
            BufferedReader fbr = new BufferedReader(new InputStreamReader(fis));
            String str = null;
            try
            {
                while ((str = fbr.readLine()) != null) {
                    list.add(Integer.parseInt(str));
                }
                fbr.close();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	 
	    try
        {
            FileOutputStream fos = view.getContext().openFileOutput("hs", Context.MODE_PRIVATE);
            BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(fos));
            try
            {
                list.add(Integer.valueOf(mScore / 10));
                Collections.sort(list, Collections.reverseOrder());
                for (int i = 0; i < Math.min(5, list.size()); i++) {
                    fw.write(list.get(i).toString() + "\n");
                }
                fw.close();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	 }
}
